﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using System.IO;

namespace ETRoboConTrakingSystem
{
    /// <summary>
    /// Interaction logic for GameConfiguration.xaml
    /// </summary>
    public partial class GameConfiguration : Window
    {
        Dictionary<string, int> dbContests = new Dictionary<string, int>();
        Dictionary<string, int> dbAreas = new Dictionary<string, int>();
        Dictionary<string, int> dbGameSpecs = new Dictionary<string, int>();

        public GameConfiguration()
        {
            InitializeComponent();

            var client = new ETRoboConRM.ETRoboConRaceMgmtClient();
            RefleshAllInfos(client);
            client.Close();
        }

        private void buttonSelectDefFile_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog dialog = new Microsoft.Win32.OpenFileDialog();
            dialog.ShowDialog();
            textBoxDefFileName.Text = dialog.FileName;
        }

        private void buttonUploadDefFile_Click(object sender, RoutedEventArgs e)
        {
            if (File.Exists(textBoxDefFileName.Text))
            {
                TextReader reader = new StreamReader(new FileStream(textBoxDefFileName.Text, FileMode.Open, FileAccess.Read));
                string content = reader.ReadToEnd();
                var client = new ETRoboConRM.ETRoboConRaceMgmtClient();
                if (client.LoadContestInfo(content))
                {

                    RefleshAllInfos(client);
                    client.Close();
                }
            }
        }

        private void RefleshAllInfos(ETRoboConRM.ETRoboConRaceMgmtClient client)
        {
            RefleshContestInfo(client);

            dbAreas.Clear();
            comboBoxAreas.Items.Clear();

            RefleshGameSpecInfo(client);
        }

        private void RefleshContestInfo(ETRoboConRM.ETRoboConRaceMgmtClient client)
        {
            dbContests.Clear();
            comboBoxContests.Items.Clear();
            ETRoboConRM.ContestInfo[] contests;
            if (client.GetContestInfo(out contests))
            {
                foreach (var c in contests)
                {
                    dbContests.Add(c.Name, c.ContestId);
                    comboBoxContests.Items.Add(c.Name);
                }
            }
        }

        private void RefleshGameSpecInfo(ETRoboConRM.ETRoboConRaceMgmtClient client)
        {
            dbGameSpecs.Clear();
            comboBoxGameSpecs.Items.Clear();
            ETRoboConRM.GameSpecInfo[] gameSpecs;
            if (client.GetGameSpecInfo(out gameSpecs))
            {
                foreach (var gs in gameSpecs)
                {
                    dbGameSpecs.Add(gs.Name, gs.GameSpecId);
                    comboBoxGameSpecs.Items.Add(gs.Name);
                }
            }
        }

        private void comboBoxContests_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int selectedContestId;
            if (dbContests.TryGetValue(comboBoxContests.SelectedItem.ToString(), out selectedContestId))
            {
                var client = new ETRoboConRM.ETRoboConRaceMgmtClient();
                RefleshAreaInfo(selectedContestId, client);
                client.Close();
            }
        }

        private void RefleshAreaInfo(int selectedContestId, ETRoboConRM.ETRoboConRaceMgmtClient client)
        {
            dbAreas.Clear();
            comboBoxAreas.Items.Clear();
            ETRoboConRM.AreaInfo[] areas;
            if (client.GetAreaInfo(out areas, selectedContestId))
            {
                foreach (var area in areas)
                {
                    dbAreas.Add(area.Name, area.AreaId);
                    comboBoxAreas.Items.Add(area.Name);
                }
            }
        }

        private void buttonCreateOrUpdate_Click(object sender, RoutedEventArgs e)
        {
            int contestId;
            int areaId;
            int gameSpecId;
            if (dbContests.TryGetValue(comboBoxContests.SelectedValue.ToString(), out contestId)
                && dbAreas.TryGetValue(comboBoxAreas.SelectedValue.ToString(), out areaId)
                && dbGameSpecs.TryGetValue(comboBoxGameSpecs.SelectedValue.ToString(), out gameSpecId) &&
                !string.IsNullOrEmpty(textBoxGameName.Text))
            {
                using (var client = new ETRoboConRM.ETRoboConRaceMgmtClient())
                {
                    int gameId;
                    if (client.RegistNewGame(out gameId, areaId, gameSpecId, textBoxGameName.Text))
                    {
                        textBlockGameId.Text = gameId.ToString();
                    }
                }
            }
        }
    }
}
